package copyengine.dataLayer.sync
{
	import copyengine.network.roomStructure.CENetworkCommandNotification;
	import copyengine.utils.CENestMediatorBasic;
	import copyengine.utils.GeneralUtils;
	import copyengine.utils.debug.CELog;

	import org.puremvc.as3.interfaces.INotification;

	public final class CEDataLayerSyncCommandMediator extends CENestMediatorBasic
	{
		public function CEDataLayerSyncCommandMediator()
		{
			super();
		}

		override public function listNotificationInterests():Array
		{
			return [
				//接收到底层发过来的LTC Command 及接收到别人发过来的命令
				CENetworkCommandNotification.LTC_DATA_LAYER_SYNC_COMMAND,
				//接收到发送同步Command 的Notification 
				CEDataLayerSyncCommandNotification.RECEIVE_SEND_P2P_COMMAND_TO_OTHER
				]
		}

		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case CEDataLayerSyncCommandNotification.RECEIVE_SEND_P2P_COMMAND_TO_OTHER:
					onReceiveSendP2PCommandToOther(notification.getBody()["notificationName"], notification.getBody()["notificationArg"]);
					break;
				case CENetworkCommandNotification.LTC_DATA_LAYER_SYNC_COMMAND:
					onReceiveLTCDataLayerSyncCommand(notification.getBody());
					break;
			}
		}

		private function onReceiveSendP2PCommandToOther(_notificationName:String, _notificationArg:Object):void
		{
			//原始的用于LTC传送的Arg Object
			var ltcArgOriginal:Object =
				{
					notificationName:_notificationName,
					notificationArg:_notificationArg
				};
			//序列化后的LTC Arg
			var ltcArgSerialize:Object = GeneralUtils.serializeObject(ltcArgOriginal);

			//发送P2P Command
			sendNotification(CENetworkCommandNotification.RECEIVE_COMMAND_SEND_DATA_TO_OTHER,
							 {
								 LTC:CENetworkCommandNotification.LTC_DATA_LAYER_SYNC_COMMAND,
								 arg:ltcArgSerialize
							 });

		}

		private function onReceiveLTCDataLayerSyncCommand(_ltcArgSerialize:Object):void
		{
			var ltcArgOriginal:Object = GeneralUtils.deserializeObject(_ltcArgSerialize);
			CELog.log("CEDataLayerSyncCommandMediator-> onReceiveLTCDataLayerSyncCommand command : " + ltcArgOriginal["notificationName"]);
			sendNotification(ltcArgOriginal["notificationName"], ltcArgOriginal["notificationArg"]);
		}


	}
}
